package com.example.mall.web.controller;

import com.example.mall.domain.OrderDetails;
import com.example.mall.dto.OrderDetailsDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.OrderDetailsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IOrderDetailsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderDetails")
public class OrderDetailsController {

    @Autowired
    public IOrderDetailsService orderDetailsService;

    /**
     * 分页查询订单明细表信息
     * @param orderDetailsQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询订单明细表信息",notes = "分页查询订单明细表信息",httpMethod = "GET")
    public JSONResult queryPage(OrderDetailsQuery orderDetailsQuery){
        PageDTO<OrderDetailsDto> orderDetailsDtoPageDTO = orderDetailsService.queryPage(orderDetailsQuery);
        return JSONResult.success("查询成功",orderDetailsDtoPageDTO);
    }

    /**
     * 根据id查询订单明细表信息
     * @param detailId
     * @return
     */
    @GetMapping("query/{detailId}")
    @ApiOperation(value = "根据id查询订单明细表信息",notes = "根据id查询订单明细表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("detailId")Long detailId){
        //1.根据id查询用户信息
        if (detailId == null){
            throw new GlobalCustomerException(ErrorEnumCode.DETAILID_NOT_ID);
        }
        OrderDetails orderDetails = orderDetailsService.getById(detailId);
        return JSONResult.success("查询成功",orderDetails);
    }

    /**
     * 根据id查询订单明细表信息
     * @param detailId
     * @return
     */
    @GetMapping("queryOrderDetailsById/{detailId}")
    @ApiOperation(value = "根据id查询订单明细表信息",notes = "根据id查询订单明细表信息",httpMethod = "GET")
    public JSONResult queryOrderDetailsById(@PathVariable("detailId")Long detailId){
        //1.根据id查询用户信息
        if (detailId == null){
            throw new GlobalCustomerException(ErrorEnumCode.DETAILID_NOT_ID);
        }
        OrderDetails orderDetails = orderDetailsService.queryOrderDetailsById(detailId);
        return JSONResult.success("查询成功",orderDetails);
    }

    /**
     * 查询全部订单明细表信息
     * @param
     * @return
     */
    @GetMapping("queryOrderDetailsList")
    @ApiOperation(value = "查询全部订单明细表信息",notes = "查询全部订单明细表信息",httpMethod = "GET")
    public JSONResult queryOrderDetailsList(){
        List<OrderDetails> logins = orderDetailsService.queryOrderDetailsList();
        return JSONResult.success("查询成功",logins);
    }

    /**
     * 新增订单明细表
     * @param orderDetailsDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增订单明细表",notes = "新增订单明细表",httpMethod = "POST")
    public JSONResult insert(@RequestBody OrderDetailsDto orderDetailsDto){
        orderDetailsService.insert(orderDetailsDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改订单明细表信息
     * @param orderDetailsDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改订单明细表信息",notes = "修改订单明细表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody OrderDetailsDto orderDetailsDto){
        orderDetailsService.update(orderDetailsDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除订单明细表信息
     * @param detailId
     * @return
     */
    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除订单明细表信息",notes = "删除订单明细表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("detailId") Long detailId) {
        orderDetailsService.delete(detailId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除订单明细表信息
     * @param detailIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除订单明细表信息",notes = "批量删除订单明细表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> detailIds) {
        orderDetailsService.deleteBatch(detailIds);
        return JSONResult.success("批量删除成功");
    }
}
